{        
Function:       _FxDeviation  

Description:    This function calculates the unit deviation from a defined reference.

                Parameters for the reference function are passed in array RefParam.
                Parameters for the deviation function are passed in array DevParam.
                
                In general, this function should be called only during BarStatus(1) = 2 conditions
                as the result values only change once per bar

Provided By:    Mark J. Krisburg

Modified:       12/30/06    original version
                05/01/09    code cleaned and streamlined
                01/08/11    slope of linear regression line made available as output variable
                01/23/11    posted to forum
                11/10/12    LinearRegFaster function substituted for LinearReg

RefID:          (0) RefPrice (input RefPrice without averaging )
                (1) Simple Moving Average 
                (2) Exponential Moving Average 
                (3) Linear Regression 
                (4) T3 Average
                (5) Jurik Moving Average
                (6) Reference Value 

DevID:          (0) Raw deviation without further processing 
                (1) Standard Deviations ( Param=SDLength )
                (2) Standard Error ( Parm = SELength )
                (3) AvgTrueRange ( Param=ATRLength )
                (4) JMA True Range ( ATR using Juric moving average )
                (5) Percent
                (6) Points
 

Usage:

Arrays:
    RefParam[4](0),                    { reference function (center line) parameters }
    DevParam[4](0);                    { deviation function (bands or ribbons) parameters }

    If BarStatus(1) = 2 then
        Value1 = _FxDeviation(Price1, Price2, RefPrice, Offset, RefParam, DevParam, oRef, oUnitDev, oLRSlope);
    
}

Inputs:
    RefPrice(NumericSeries),
    Offset(NumericSimple),

    { input arrays }
    RefParam[xx](NumericArrayRef),
    DevParam[yy](NumericArrayRef),

    { outputs }
    oRef(NumericRef), 
    oUnitDev(NumericRef),
    oLRSlope(NumericRef);

Variables:     
    RefID(0),                    { reference function ID }
    R1(0),                       { reference function parameters }
    R2(0),
    R3(0),
    R4(0),
    DevID(0),                    { deviation function ID }
    D1(0),                       { deviation function paramaters }
    D1b(0),                      { special handling required for standard dev and standard error functions }                
    D2(0),
    D3(0),
    D4(0),
    
    oLRAngle(0),
    oLRIntercept(0),
    oMean(0);


{ unpack reference and deviation function parameters }
        
once begin

    RefID = RefParam[0];
    R1 = RefParam[1];
    R2 = RefParam[2];
    R3 = RefParam[3];
    R4 = RefParam[4];

    DevID = DevParam[0];
    D1 = DevParam[1];
    D1b = MaxList(2, D1);        { special handling required for standard dev and standard error functions }    
    D2 = DevParam[2];
    D3 = DevParam[3];
    D4 = DevParam[4];

end;

    
    switch RefID begin  
        Case 0:
            oRef = RefPrice[Offset];
        Case 1:
            oRef = Average(RefPrice[Offset], R1);
        Case 2:
            oRef = XAverage(RefPrice[Offset], R1);
        Case 3:
            value1 = _LinearRegFaster( RefPrice[Offset], R1, R2, oLRSlope, oLRAngle, oLRIntercept, oRef) ;            
        Case 4:
            oRef = AdaptiveMovAvg(RefPrice[Offset],  R1, R2, R3);
        Case 5:
            oRef = T3Average.series(RefPrice[Offset], R1);
        
        // uncomment the next two lines if you have purchased Jurik Moving Average functions
//        Case 6:    
//            oRef = JRC.JMA.2K(RefPrice[Offset], R1, R2);
        Case 7:
            oRef = _VWAP;
        Case 8:
            oRef = R1;
    end;

    switch DevID begin
        Case 0:                    
            oUnitDev = 1;            { raw deviation without processing }
        Case 1:
            value1 = _StdDevMean(RefPrice[Offset], D1b, 1, oMean, oUnitDev);
        Case 2:
            oUnitDev = _StdError(RefPrice[Offset], D1b);
        Case 3:
            oUnitDev = _AvgTrueRange(D1)[Offset];
        
        // uncomment this and the next line if you have purchased Jurik Moving Average functions
//        case 4:
//            oUnitDev = _JMATrueRange(D1)[Offset];
        Case 5:
            oUnitDev = RefPrice[Offset]*D1/100; 
        Case 6:
            oUnitDev = R1;
    end;
    
    _FxDeviation = oUnitDev;  
